//
//  AreasConcern.m
//  HappyStore
//
//  Created by 李小辉 on 16/6/29.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "AreasConcern.h"

@interface AreasConcern ()
@property (strong, nonatomic) NSArray *aressAry;
@property (strong, nonatomic) NSMutableArray *btnAry;
@end

@implementation AreasConcern

- (NSMutableArray *)btnAry{
    if (!_btnAry) {
        _btnAry = [NSMutableArray array];
    }
    return _btnAry;
}

- (NSArray *)aressAry{
    if (!_aressAry) {
        _aressAry = @[@"生物医药", @"旅游休闲", @"制造业", @"批发零售",
                      @"房地产业",@"交通运输", @"冶金矿产", @"建筑建材",
                      @"信息软件", @"金融", @"教育", @"现代服务业",
                      @"生态环保", @"市政基建", @"文体娱乐", @"社会保障",
                      @"水电燃气", @"农林牧渔", @"其他"];
    }
    return _aressAry;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.translucent = YES;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"白色背景"] forBarMetrics:UIBarMetricsDefault];
    //找出导航栏的下边的黑线
    UIImageView *navBarHairlineImageView = [NaviBarLine findHairlineImageViewUnder:self.navigationController.navigationBar];
    navBarHairlineImageView.hidden = NO;
    navBarHairlineImageView.alpha = 0.5f;
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.translucent = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 190, 44)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.font = [UIFont systemFontOfSize:19];
    titleLabel.text = self.titleString;
    titleLabel.textColor = RGB(45, 45, 45);
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;
    
    //设置返回按钮
    UIBarButtonItem *leftItem = [UIBarButtonItem itemWithTarget:self action:@selector(leftBarButtonItemClick) image:@"返回" highImage:@"返回"];
    self.navigationItem.leftBarButtonItem = leftItem;
    self.view.backgroundColor = [UIColor whiteColor];
    self.tabBarController.tabBar.hidden = YES;
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(chickrightItem)];
    rightItem.tintColor = RGB(45, 45, 45);
    self.navigationItem.rightBarButtonItem = rightItem;
    
    [self addAreasBtn];
    
}

//添加按钮
- (void)addAreasBtn{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(8, 70, 100, 30)];
    label.text = @"最多可选3个";
    label.textColor = [UIColor blackColor];
    label.font = [UIFont systemFontOfSize:16];
    [self.view addSubview:label];
    for (int i = 0; i<self.aressAry.count; i++) {
        //创建按钮
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(i%3*85 + 38, i/3*40 + 100, 75, 30)];
        [button setTitle:self.aressAry[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor colorWithRed:137/255.0f green:137/255.0f blue:137/255.0f alpha:1.0f] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:13.0f];
        [button addTarget:self action:@selector(btnClickAreasBrn:) forControlEvents:UIControlEventTouchUpInside];
        button.layer.masksToBounds = YES;
        button.layer.borderColor = [UIColor colorWithRed:201/255.0f green:202/255.0f blue:202/255.0f alpha:1.0f].CGColor;
        button.layer.borderWidth = 1.0f;
        button.layer.cornerRadius = 5.0f;
        [self.view addSubview:button];
    }
    
    
}


//按钮事件
- (void)btnClickAreasBrn:(UIButton *)sender{
    
    if (sender.selected) {
        sender.selected = NO;
        [sender setTitleColor:RGB(137, 137, 137) forState:UIControlStateNormal];
        sender.layer.borderColor = RGB(201, 202, 202).CGColor;
        [self.btnAry removeObject:sender];
    }else{
        sender.selected = YES;
        [sender setTitleColor:RGB(255, 107, 80) forState:UIControlStateNormal];
        sender.layer.borderColor = RGB(255, 107, 80).CGColor;
        if (self.btnAry.count == 3) {
            UIButton *button = (UIButton *)[self.btnAry firstObject];
            button.selected = NO;
            [button setTitleColor:RGB(137, 137, 137) forState:UIControlStateNormal];
            button.layer.borderColor = RGB(201, 202, 202).CGColor;
            [self.btnAry removeObject:[self.btnAry firstObject]];
        }
        [self.btnAry addObject:sender];
    }
    
    
}

- (void)chickrightItem{

    NSString *string = @"";
    for (UIButton *btn in self.btnAry) {
        string = [string stringByAppendingString:[NSString stringWithFormat:@"%@,",btn.titleLabel.text]];
    }
    string = [string substringWithRange:NSMakeRange(0, string.length -1)];
    
    NSDictionary *dic = @{@"string":string};
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"AreasConcernStr" object:nil userInfo:dic];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)leftBarButtonItemClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}


@end
